package com.example.waterorder.controller;

import com.example.waterorder.entity.Order;
import com.example.waterorder.entity.constant.WaterConstant;
import com.example.waterorder.dto.BuildingOrder;
import com.example.waterorder.tools.Result;
import com.example.waterorder.entity.constant.WaterMessage;
import com.example.waterorder.service.OrderService;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {

    @Resource
    private OrderService orderService;
    @Resource
    private RabbitTemplate rabbitTemplate;

    private String exchangeName = "order.exchange";

    @PostMapping("/create")
    @Operation(summary = "用户创建订单")
    public Result createOrder(@RequestBody Order order) {

        Integer flag = orderService.createOrder(order);
        // 发送消息到消息队列
        if (flag == 1) {
            rabbitTemplate.convertAndSend(exchangeName, WaterMessage.KEY_WORKER, WaterMessage.ORDER_NEW);
        }

        return flag == 1 ? new Result(true, WaterMessage.ORDER_CREATE_SUCCESS)
                : new Result(false, WaterMessage.ORDER_CREATE_FAILD);
    }

    @GetMapping("/select/byUser")
    @Operation(summary = "根据用户id查询所有订单")
    public Result selectOrderByUserId
            (@RequestParam("userId") String userId,
             @RequestParam(name = "delivery", required = false) Integer delivery) {

        List<Order> orderList = orderService.selectOrderByUserId(userId, delivery);
        return orderList != null ? new Result(true, orderList)
                : new Result(false);
    }

    @GetMapping("/select/byWorker")
    @Operation(summary = "送水员查看订单")
    public Result selectOrderByBuildingNotDelivery
            (@RequestParam("workerId") String workerId,
             @RequestParam(name = "building", required = false) String building,
             @RequestParam(name = "delivery", required = false) Integer delivery) {

        List<Order> orderList = orderService.selectOrderByWorker(workerId, building, delivery);
        return orderList != null ? new Result(true, orderList)
                : new Result(false);
    }

    @GetMapping("/select/today")
    @Operation(summary = "送水员查询今天的所有订单")
    public Result selectOrderToday(@RequestParam("workerId") String workerId,
                                   @RequestParam(name = "delivery",required = false) String delivery) {

        List<Order> orderList = orderService.selectOrderToday(workerId,delivery);
        return orderList != null ? new Result(true, orderList)
                : new Result(false);
    }

    @GetMapping("/select/accept")
    @Operation(summary = "送水员查看所有待接单的二级订单,用于接单大厅")
    public Result selectBuildingOrderNotAccept(@RequestParam("lng") String lng,
                                               @RequestParam("lat") String lat) {
        List<BuildingOrder> buildingOrderList = orderService.selectBuildingOrderNotAccept(lng,lat);
        return buildingOrderList != null ? new Result(true, buildingOrderList)
                : new Result(false);
    }

    @GetMapping("/select/delivery")
    @Operation(summary = "送水员查看所有自己已接单未送达的二级订单,用于送单大厅")
    public Result selectBuildingOrderNotDelivery(@RequestParam("workerId") String workerId,
                                                 @RequestParam("lng") String lng,
                                                 @RequestParam("lat") String lat){
        List<BuildingOrder> buildingOrderList = orderService.selectBuildingOrderNotDelivery(
                workerId,lng,lat);
        return buildingOrderList != null ? new Result(true, buildingOrderList)
                : new Result(false);
    }

    @GetMapping("/quantity")
    @Operation(summary = "查询水桶数")
    public Result selectBuildingQuantity
            (@RequestParam("workerId") String workerId,
             @RequestParam(name = "building", required = false) String building,
             @RequestParam(name = "delivery", required = false) Integer delivery) {

        Integer buildingQuantity = orderService.selectQuantity(workerId, building, delivery);
        return buildingQuantity != null ? new Result(true, buildingQuantity)
                : new Result(false);
    }

    @GetMapping("/quantity/today")
    @Operation(summary = "查询今日售出且送达桶数")
    public Result selectQuantityToday(@RequestParam("workerId") String workerId) {
        Integer buildingQuantity = orderService.selectQuantityToday(workerId);
        return buildingQuantity != null ? new Result(true, buildingQuantity)
                : new Result(false);
    }

    @GetMapping("/amount")
    @Operation(summary = "送水员根据time不同查询不同时间前的营业额(已送达)")
    public Result selectAmountByTime(@RequestParam("workerId") String workerId,
                                     @RequestParam(name = "time", required = false) String time) {
        if (time == null || time.isEmpty() || time.equals("total")) {
            Double totalAmount = orderService.selectTotalAmount(workerId);
            return totalAmount != null ? new Result(true, totalAmount)
                    : new Result(false);
        }
        if (time.equals("day")) {
            Double todayAmount = orderService.selectTodayAmount(workerId);
            return todayAmount != null ? new Result(true, todayAmount)
                    : new Result(false);
        }
        if (time.equals("week")) {
            Double weeklyAmount = orderService.selectWeeklyAmount(workerId);
            return weeklyAmount != null ? new Result(true, weeklyAmount)
                    : new Result(false);
        }
        if (time.equals("month")) {
            Double monthlyAmount = orderService.selectMonthlyAmount(workerId);
            return monthlyAmount != null ? new Result(true, monthlyAmount)
                    : new Result(false);
        }
        return new Result(false);
    }

    @PutMapping("/update")
    @Operation(summary = "修改订单信息")
    public Result updateOrder(@RequestBody Order order) {
        Integer flag = orderService.updateOrder(order);
        return flag == 1 ? new Result(true, WaterMessage.UPDATE_SUCCESS)
                : new Result(false, WaterMessage.UPDATE_FAILD);
    }

    @GetMapping("/accept/byBuilding")
    @Operation(summary = "送水员按宿舍楼接单")
    public Result acceptOrderByBuilding(@RequestParam("building") String building
            , @RequestParam("workerId") String workerId) {
        List<Order> orderList = orderService.selectOrder(building, 0);
        Integer flag = 0;
        for (Order order : orderList) {
            order.setWorkerId(workerId);
            order.setStateDelivery(WaterConstant.ORDER_ACCEPT_STATE);
            flag = orderService.updateOrder(order);
        }

        // 发送消息到消息队列
        if (flag == 1) {
            rabbitTemplate.convertAndSend(exchangeName, WaterMessage.KEY_USER, WaterMessage.ORDER_ACCEPT);
        }
        return flag == 1 ? new Result(true, WaterMessage.ORDER_ACCEPT_SUCCESS)
                : new Result(false, WaterMessage.ORDER_ACCEPT_FAILD);
    }

    @GetMapping("/finish/byBuilding")
    @Operation(summary = "送水员按宿舍楼完成订单")
    public Result finishOrderByBuilding(@RequestParam("building") String building
            , @RequestParam("workerId") String workerId) {
        List<Order> orderList = orderService.selectOrderByWorker(workerId, building, 1);
        Integer flag = 0;
        for (Order order : orderList) {
            order.setStateDelivery(WaterConstant.ORDER_DELIVERY_STATE);
            flag = orderService.updateOrder(order);
        }

        // 发送消息到消息队列
        if (flag == 1) {
            rabbitTemplate.convertAndSend(exchangeName, WaterMessage.KEY_USER, WaterMessage.ORDER_ACCEPT);
        }
        return flag == 1 ? new Result(true, WaterMessage.ORDER_FINISH_SUCCESS)
                : new Result(false, WaterMessage.ORDER_FINISH_FAILD);
    }
}
